package cn.tedu.ivos2.audit.service.Impl;

import cn.tedu.ivos2.application.mapper.ApplicationMapper;
import cn.tedu.ivos2.application.pojo.entity.Application;
import cn.tedu.ivos2.audit.mapper.AuditMapper;
import cn.tedu.ivos2.audit.pojo.dto.AuditQuery;
import cn.tedu.ivos2.audit.pojo.dto.AuditSaveDTO;
import cn.tedu.ivos2.audit.pojo.entity.Audit;
import cn.tedu.ivos2.audit.pojo.vo.AuditVO;
import cn.tedu.ivos2.audit.service.AuditService;
import cn.tedu.ivos2.base.enums.ApplicationStatusEnum;
import cn.tedu.ivos2.base.enums.AuditStatusEnum;
import cn.tedu.ivos2.user.mapper.UserMapper;
import cn.tedu.ivos2.user.pojo.vo.UserLoginVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.constraints.Size;
import java.util.ArrayList;
import java.util.List;
import java.util.StringJoiner;

@Slf4j
@Service
@Transactional
public class AuditServiceImpl implements AuditService {
    @Autowired
    private AuditMapper auditMapper;
    @Autowired
    private ApplicationMapper applicationMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public void saveAudit(Application application) {
        log.debug("审批_新增审批单数据:{}",application);
        List<Long> auditUserIdList=application.getAuditUserIdList();
        //根据审核id集合获取审核信息
        for (int i=0;i<auditUserIdList.size();i++){
//            UserLoginVO auditUser = userMapper.selectById(auditUserIdList.get(i));
            Audit audit=new Audit();
            audit.setApplicationId(application.getId());
            audit.setAuditUserId(auditUserIdList.get(i));
            //第一个人式直接上级,由他第一个审批
            if (i==0){
                audit.setAuditStatus(AuditStatusEnum.MY_PENDING.getCode());
            }else {
                audit.setAuditStatus(AuditStatusEnum.PENDING.getCode());
            }
            //设置审批的顺序,上级是0,上级的上级是1
            audit.setAuditSort(i);
            auditMapper.saveAudit(audit);
        }

    }

    @Override
    public List<AuditVO> select(AuditQuery auditQuery) {
        log.debug("查询审批单:{}",auditQuery);
        List<AuditVO>auditVOList=auditMapper.selectAudit(auditQuery);
        //循环取出审批单
        for (int i=0;i<auditVOList.size();i++){
            AuditVO auditVO=auditVOList.get(i);
            //准备审批人的数据
            assignAuditUserList(auditVO);
        }
        return auditVOList;
    }

    /**
     * 更新审批单的状态
     * @param auditSaveDTO
     */
    @Override
    public void update(AuditSaveDTO auditSaveDTO) {
        Audit audit=new Audit();
        BeanUtils.copyProperties(auditSaveDTO,audit);
        //审批单状态更新,申请单也要更改
        Application application=new Application();
        application.setId(audit.getApplicationId());
        //如果是审批单是30,则更新
        if (audit.getAuditStatus().equals(AuditStatusEnum.AUDITED.getCode())){
            //把当前审核单的状态更改为30已审核
            auditMapper.update(audit);
            //查询还有剩几条需要更新(申请单id相同,状态不为30的的审核单)
            AuditQuery auditQuery=new AuditQuery();
            auditQuery.setApplicationId(audit.getApplicationId());
            Integer count=auditMapper.selectRestAuditCount(auditQuery);
            if (count>0){
                auditQuery.setAuditSort(audit.getAuditSort()+1);
                //查询下一条审核单数据
                List<AuditVO>auditVOList=auditMapper.selectAudit(auditQuery);
                if (auditVOList!=null && auditVOList.size()>0){
                    //下一条审核单数据
                    AuditVO auditVO=auditVOList.get(0);
                    //准备更新这条数据的状态
                    Audit audit1=new Audit();
                    audit1.setId(auditVO.getId());
                    audit1.setAuditStatus(AuditStatusEnum.MY_PENDING.getCode());
                    auditMapper.update(audit1);
                }
                //更新申请单的状态
                application.setStatus(ApplicationStatusEnum.AUDIT.getCode());
                applicationMapper.update(application);
            }else {
                //如果只有一级审批,直接更改
                application.setStatus(ApplicationStatusEnum.AUDITED.getCode());
                applicationMapper.update(application);
            }
        }else if(audit.getAuditStatus().equals(AuditStatusEnum.REJJECT.getCode())){
            //状态码40,驳回申请,更新当前的审核单
            auditMapper.update(audit);
            AuditQuery auditQuery=new AuditQuery();
            auditQuery.setApplicationId(audit.getApplicationId());
            //获取所有审核单
            List<AuditVO> auditVOList = auditMapper.selectAudit(auditQuery);
            //判断是否还有审批
            if (auditVOList!=null && auditVOList.size()>0){
                for (int i=0;i<auditVOList.size();i++){
                    //审核单的每一条
                    AuditVO auditVO=auditVOList.get(i);
                    //由我审核的审核单状态被修改为驳回,剩下的待审核不需要修改,直接删除
                    if (AuditStatusEnum.PENDING.getCode().equals(auditVO.getAuditStatus())){
                        auditMapper.deleteByAuditId(auditVO.getId());
                    }
                }
            }
            //更新申请单状态
            application.setStatus(ApplicationStatusEnum.REJECT.getCode());
            application.setRejectReason(auditSaveDTO.getRejectReason());
            applicationMapper.update(application);

        }
    }

    /**
     * 查询申请单数据
     * @param auditVO
     */
    private void assignAuditUserList(AuditVO auditVO) {
        //空集合存放数据
        List<String>auditUsernameList=new ArrayList<>();
        List<Long>auditUserIdList=new ArrayList<>();
        //查询audit与application表获得所有数据
        List<AuditVO> list = auditMapper.selectAuditByApplicationId(auditVO.getApplicationId());
        //遍历集合,取出审批人id,通过id查询username
        for (int i=0;i<list.size();i++){
            Long userId=list.get(i).getAuditUserId();
            auditUserIdList.add(userId);
            UserLoginVO user = userMapper.selectById(userId);
            auditUsernameList.add(user.getUsername());
        }
        //返回查询结果
        auditVO.setAuditUserIdList(auditUserIdList);
        //把username变成以逗号分割的字符串
        StringJoiner stringJoiner=new StringJoiner(",");
        for (String username:auditUsernameList){
            stringJoiner.add(username);
        }
        auditVO.setAuditUsernameList(stringJoiner.toString());
    }
}
